var app = getApp();
var ux = require('../../../../lib/ux.js');
var QRCode = require('../../../../utils/weapp-qrcode.js')
var qrcode;

const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
// 300rpx 在6s上为 150px
const code_w = 460 / rate; 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qxOk:false,
    sums: 0,
    thisItemID:null,
    cacount:0,
    list:[],
    item:{},
    thisImg: "",
    image: '',
    code_w: code_w,
    qxUrl: "/api/orders/cancelOrder.do",
    shUrl: "/api/orders/getGoodsOk.do",
    tkUrl: "/api/orders/returnMoney.do",
    tsUrl: "/api/orders/tSOrder.do",
    shouhuo: false,
    isShow: true,
    hexiao: false,
    time:null,
    tousus: '',
    tuikuanliyouo: '',
    tousu: false,
    tuikuan: false,
    orderQ: false,
    tel: '15888888888',
    lianxi: false,
    dot:{},
    winheight: 0,
    winwidth: 0,
    action: "/api/balance/getOrderDetailInfos.do",
  },
  toShopDetail: function (event) {
    var dataset = event.currentTarget.dataset;
    var shopid = dataset.shopid;
    var price=dataset.price;
    wx.navigateTo({
      url: '/pages/member/tab1/shop/detail?id=' + shopid +'&prices='+price,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    this.setData({
      idx:options.idx,
      thisItemID:options.id,
      merchant: wx.getStorageSync("banrdId")
    })
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
    qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: "kong",
      image: '/lib/dute.mp/res/bbv.png',
      width: code_w,
      height: code_w,
      colorDark: "#000000",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
    console.log(options.id+"ddddddddddddddddddddd");
    var params = { orderId: parseInt(options.id) }
    ux.request(this.data.action, params, function (protocol) {
      wx.hideLoading();
      console.log(JSON.stringify(protocol.data))
      var cars = protocol.data.list;
      var cacount=0;
      var sums=0;
      for (var i = 0; i < cars.length; i++) {
        sums = ux.acAdd(sums, ux.acMul(cars[i].number, cars[i].price));
        cacount = parseInt(cacount + cars[i].number)
      }
      self.setData({
        sums: sums,
        cacount: cacount,
        list: protocol.data.list,
        dot: protocol.data.dot,
        item: protocol.data.order,
        time: ux.formatTime(new Date(protocol.data.order.addTime))
      })
    }, true);
  },
  preventTouchMoveTy: function () {
    return;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },
  onLreload:function(){
    var params = { orderId: parseInt(this.data.thisItemID) }
    ux.request(this.data.action, params, function (protocol) {
      wx.hideLoading();
      console.log(JSON.stringify(protocol.data))
      var cars = protocol.data.list;
      var cacount=0;
      var sums=0;
      for (var i = 0; i < cars.length; i++) {
        sums = ux.acAdd(sums, ux.acMul(cars[i].number, cars[i].price));
        cacount = parseInt(cacount + cars[i].number)
      }
      self.setData({
        sums: sums,
        cacount: cacount,
        list: protocol.data.list,
        dot: protocol.data.dot,
        item: protocol.data.order,
        time: ux.formatTime(new Date(protocol.data.order.addTime))
      })
    }, true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
  cancelTel: function () {
    this.setData({
      lianxi: false
    })
  },
  tel: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.tel,
    })
  },
  openTel: function () {
    this.setData({
      lianxi: true
    })
  },
  orderQOpen: function (event) {
    this.setData({
      thisItemID: this.data.id,
      orderQ: true
    })
  },
  orderQcancel: function () {
    this.setData({
      orderQ: false
    })
  },
  tuikuanOk: function () {
    var self = this;
    if (!this.data.tuikuanliyouo) {
      self.toast("请输入退款理由");
      return false;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })

    var parmas = { id: this.data.thisItemID, liyou: self.data.tuikuanliyouo };
    ux.request(this.data.tkUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          tuikuanliyouo: "",
          tuikuan: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          tuikuanliyouo: "",
          tuikuan: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  tousuOk: function () {
    var self = this;
    if (!this.data.tousus) {
      self.toast("请输入投诉理由");
      return false;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var parmas = { id: this.data.thisItemID, liyou: self.data.tousus };
    ux.request(this.data.tsUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          tousus: "",
          tousu: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          tousus: "",
          tousu: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  QX: function () {
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var obj = this.data.item;
    var parmas = { id: parseInt(obj.id) };
    ux.request(this.data.qxUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
  wx.switchTab({
    url: '/pages/member/tab3/MyCenter',
  })

        obj.status=7;
        self.setData({
          obj:obj,
          qxOk:true,
          orderQ: false,
          isShow: true
        })
        self.toast(data.data.msg);
      } else {
        self.setData({
          orderQ: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  gotoPayPage: function (event) {
    var obj = this.data.item;
    var urls = '';
    if (obj.orderType == 1) {//直接购买订单
      this.setData({
        isShow: true
      })
      urls = '/pages/member/tab2/confirmZhiOrder?orderId=' + obj.id;
    } else {
      this.setData({
        isShow: true
      })
      urls = '/pages/member/tab2/confirmOrder?orderId=' + obj.id;
    }
    wx.navigateTo({
      url: urls,
    })
  },
  opentousu: function (event) {
    this.setData({
      thisItemID: this.data.id,
      tousu: true
    })
  },
  xtousu: function () {
    this.setData({
      tousus: "",
      tousu: false
    })
  },
  opentuikuan: function (event) {
    this.setData({
      thisItemID: this.data.id,
      tuikuan: true
    })
  },
  xtuikuan: function () {
    this.setData({
      tuikuanliyouo: "",
      tuikuan: false
    })
  },
  tuikuanliyouo: function (e) {
    this.setData({
      tuikuanliyouo: e.detail.value
    })
  },
  tousus: function (e) {

    this.setData({
      tousus: e.detail.value
    })
  },
  openxhexiao: function (event) {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      thisItemID: this.data.id,
      thisImg: this.data.item.no,
      hexiao: true
    })
    qrcode.makeCode(this.data.thisImg)
    wx.hideLoading();
  },
  xhexiao: function () {
    this.setData({
      hexiao: false
    })
  },
  hexiaoOk: function () {//核销完毕刷新页面
    this.setData({
      hexiao: false,
      isShow: true
    })
    this.onShow();
  },
  shouhuoOpen: function (event) {
  
    this.setData({
      thisItemID: this.data.id,
      shouhuo: true
    })
  },
  shouhuoOk: function () {//掉后台确认收货
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { id: this.data.thisItemID };
    ux.request(this.data.shUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          shouhuo: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          shouhuo: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  shouhuocancel: function () {
    this.setData({
      shouhuo: false
    })
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  gotoDao: function () {
    var item = this.data.dot;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图定位功能将无法使用',
            success: function (res) {
              if (res.cancel) {
                console.info("1授权失败返回数据");

              } else if (res.confirm) {
                //village_LBS(that);
                wx.openSetting({
                  success: function (data) {
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      wx.openLocation({
                        latitude: parseFloat(item.lan),
                        longitude: parseFloat(item.long),
                        name: item.address,
                        scale: 28
                      })
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            }
          })
        } else {
          wx.openLocation({
            latitude: parseFloat(item.lan),
            longitude: parseFloat(item.long),
            name: item.address,
            scale: 28
          })
        }
      }
    })
  }

})